<?php
/**
 * Hello Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license    GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
include_once(JPATH_COMPONENT.DS.'helpers'.DS.'pear_calendar.php');
class calendarModelcalendar extends JModel
{
	 var $_data;
	 var $_buildtype;
	 var $cal_query;
	 //var_dump('here');
	 function _setBuild($type = 'Calendar')
    {
		$this->_buildtype = "_build".ucfirst($type);
		//var_dump($this->_buildtype);
		return "_build".ucfirst($type);
	}
	 
    function _buildCalendar()
    {
        //$query = 'SELECT a.id, a.title, a.start_date, a.start_time, a.introdescription, a.catid, a.params, c.params as cat_params  FROM #__calendar AS a INNER JOIN #__categories AS c ON a.catid = c.id WHERE a.published = 1 ORDER BY a.catid, a.start_date' ;
        $query = 'SELECT a.id, a.title, a.start_date, a.start_time, a.introdescription, a.catid, a.params, c.params as cat_params  FROM #__calendar AS a INNER JOIN #__categories AS c ON a.catid = c.id WHERE a.state = 1 ORDER BY a.catid, a.start_date' ;
        return $query;
    }
	
	function _buildDetail()
    {
        //$query = 'SELECT a.id, a.title, a.start_date, a.start_time, a.description, a.catid  FROM #__calendar AS a INNER JOIN #__categories AS c ON a.catid = c.id WHERE a.published = 1 and a.id=".." ORDER BY a.catid, a.start_date' ;
        $query = 'SELECT a.id, a.title, a.start_date, a.start_time, a.description, a.catid  FROM #__calendar AS a INNER JOIN #__categories AS c ON a.catid = c.id WHERE a.state = 1 and a.id=".." ORDER BY a.catid, a.start_date' ;
        return $query;
    }

    /**
     * Retrieves the hello data
     * @return array Array of objects containing the data from the database
     */
    function getData($date_obj = NULL)
    {
        // Lets load the data if it doesn't already exist
        if (empty( $this->_data ))
        {
			if(empty($this->_buildtype)){
				$type = $this->_setBuild();
			}else{
				$type = $this->_buildtype;
			}
            $calendar = $this->$type();//$this->_buildCalendar();////$this->_build$type();
		//	$this->cal_query = $this->$type();
            $this->_data =& $this->_getList( $calendar );
			
			if(count($this->_data > 0)){
			foreach($this->_data as $id){
				$reg = new JRegistry('cat_data_params');
				$reg->loadINI($id->cat_params,'cat_data_params');
				$obj =& $reg->toObject('cat_data_params');
				$new = new stdclass();
				foreach($obj as $key=>$value){
					$new->params->$key = $value;
				}//end foreach
				$id->cat_params = $new->params;
			}//end foreach
		}//end if
        }
		
		return $this->_data;
		
    }
	
	function diary(){
		// Create a day to view the hours for
			$Day = & new Calendar_Day(2003,10,24);
			
			$result = array (
				array('eventtime'=>mktime(9,0,0,10,24,2003),'entry'=>'Meeting with sales team'),
				array('eventtime'=>mktime(11,0,0,10,24,2003),'entry'=>'Conference call with Widget Inc.'),
				array('eventtime'=>mktime(15,0,0,10,24,2003),'entry'=>'Presentation to board of directors')
				);
			
			// An array to place selected hours in
			$selection = array();
			
			// Loop through the "database result"
			foreach ( $result as $row ) {
				$Hour = new Calendar_Hour(2000,1,1,1); // Create Hour with dummy values
				$Hour->setTimeStamp($row['eventtime']); // Set the real time with setTimeStamp
			
				// Create the decorator, passing it the Hour
				$DiaryEvent = new DiaryEvent($Hour);
			
				// Attach the payload
				$DiaryEvent->setEntry($row['entry']);
			
				// Add the decorator to the selection
				$selection[] = $DiaryEvent;
			}
			
			// Build the hours in that day, passing the selection
			$Day->build($selection);
			//var_dump($Day);
			/**/
			return $Day;
	}//end diary
	
	/**
	*	creates a decorated Month object with event data in Days
	*	@var db result set of events(to insert into days)
	*  	@var date_obj is simply an obj with YMD properties
	*/
	function diary2(&$result_set,$date_obj = NULL ){
		// faux db results
		if($result_set == NULL){
			$events = array ();
		}else{
			$events = $result_set;
		}
		
		($date_obj->year == NULL)?	$date_obj->year = date('Y'): $date_obj->year = $date_obj->year;
		($date_obj->month == NULL)?	$date_obj->month = date('m'): $date_obj->month = $date_obj->month;
		($date_obj->day == NULL)?	$date_obj->day = date('d'): $date_obj->day = $date_obj->day;
		
		/**
		*	TODO: validate $date_obj
		*/
	
		$return_obj =& new stdclass();		
		$return_obj->month =& new Calendar_Month_Weekdays($date_obj->year, $date_obj->month, 0);
		
		$monthDecorator =& new MonthPayload_Decorator2($return_obj->month);
		$monthDecorator->build($events);
		
		$return_obj->textual =& new Calendar_Decorator_Textual($return_obj->month);
		$return_obj->uri =& new Calendar_Decorator_Uri($return_obj->month);
		//$this->yearOfMonth[] = $month->thisYear();
		// build the month
		//$month->build($selectedDays);
		
		// Fetch all days in the month object
		$return_obj->daysInMonth =& $monthDecorator->fetchAll();
		return $return_obj;
	}
	
}
?>